package bm43;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/8/9 17:44
 * BM43. 包含 min 函数的栈
 * https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49?tpId=295&tqId=23268&ru=%2Fpractice%2F54275ddae22f475981afa2244dd448c6&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj
 */
public class Solution {

    Deque<Integer> dataStack = new ArrayDeque<>(); // 数据栈
    Deque<Integer> minStack = new ArrayDeque<>(); // 维护最小值


    public void push(int val) {
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
        }
        dataStack.push(val);
    }

    public void pop() {
        if (dataStack.isEmpty() || minStack.isEmpty()) {
            return;
        }
        int val = dataStack.pop();
        if (!minStack.isEmpty() && val == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        return dataStack.isEmpty() ? -1 : dataStack.peek();
    }

    public int min() {
        return minStack.isEmpty() ? - 1 : minStack.peek();
    }
}
